﻿using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Windows.Forms;
using Moonlite.Composition;
using Moonlite.ExtensionApi.ControlInterfaces;
using Moonlite.ExtensionApi.GlobalInterfaces;
using Moonlite.Platform.General;

namespace Moonlite.Modules.Editors.ImageViewer
{
    [Export("Moonlite.Composition.WindowExtensionContract", typeof(UserControl))]
    [WindowMetadata(Dock = DockStyle.Bottom, DockId = 0, Flags = WindowFlags.None, Hidden = true, Name = "ProjectManagerExtensionRegisterer")]
    public class ProjectManagerExtensionRegisterer : UserControl
    {
        private static void ImageFileOpened(IProjectFile openedFile, TreeNode node)
        {
            Viewer viewer = new Viewer();
            viewer.Open(openedFile.Path);
            Global.Instance.OpenDocument(Path.GetFileName(openedFile.Path), viewer);
        }

        private static void RegisterImageViewerLoaders()
        {
            ImageViewerLoader.Register(new BmpImageViewerLoader());
            ImageViewerLoader.Register(new DdsImageViewerLoader());
            ImageViewerLoader.Register(new ExifImageViewerLoader());
            ImageViewerLoader.Register(new GifImageViewerLoader());
            ImageViewerLoader.Register(new JpgImageViewerLoader());
            ImageViewerLoader.Register(new PngImageViewerLoader());
            ImageViewerLoader.Register(new TgaImageViewerLoader());
            ImageViewerLoader.Register(new TiffImageViewerLoader());
        }

        public ProjectManagerExtensionRegisterer()
        {
            RegisterImageViewerLoaders();
            IProjectManager projectManager = ((IProjectManager)((object[])(Global.Instance.GetService(typeof(IProjectManager))))[0]);
            foreach (string extension in ImageViewerLoader.GetRegisteredExtensions())
            {
                projectManager.AddFileExtensionAssociation(extension, ImageFileOpened);
            }
        }
    }
}
